﻿using Buddy.Coroutines;
using ff14bot;
using ff14bot.Enums;
using ff14bot.Helpers;
using ff14bot.Managers;
using ff14bot.Objects;
using MoreDots.SpellNames.Classes;
using MoreDots.SpellNames.Jobs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace MoreDots.SpellNames
{
    public class Spell
    {
        public string Name { get; set; }
        public int Level { get; set; }

        #region Casting Methods

        public string _lastSpell;
        public string LastSpell
        {
            get
            {
                if (_lastSpell == null)
                {
                    _lastSpell = string.Empty;
                }
                return _lastSpell;
            }
            private set
            {
                _lastSpell = value;
            }
        }

        public async Task<bool> Cast(GameObject target = null, bool onLocation = false)
        {
            bool CastedSpell = false;
            bool CanCast;

            if (target == null)
                target = Core.Player.CurrentTarget;

            if (target == Core.Player)
                CanCast = CheckCanCast(target, true);
            else
                CanCast = CheckCanCast(target);

            if (!CanCast)
                return false;

            if (onLocation)
            {
                if (Actionmanager.DoActionLocation(this.Name, target.Location))
                {
                    CastedSpell = true;
                }
            }
            else
            {
                if (Actionmanager.DoAction(this.Name, target))
                {
                    CastedSpell = true;
                }
            }
            if (CastedSpell)
            {
                this.LastSpell = this.Name;
                Logging.Write(Colors.OrangeRed, "[MoreDots] Ability: " + this.Name);
            }

            await Coroutine.Sleep(30);

            return CastedSpell;
        }

        public async Task<bool> CastOnLocation()
        {
            return await Cast(onLocation: true);
        }

        public async Task<bool> Buff()
        {
            return await Cast(Core.Player);
        }


        #region Helpers
        private bool CheckCanCast(GameObject target, bool isBuff = false)
        {
            if (Core.Player.IsCasting)
                return false;

            if (!Actionmanager.CanCast(this.Name, target))
                return false;

            if (isBuff)
            {
                SpellData globalCoolDown;

                Actionmanager.CurrentActions.TryGetValue(GetDefaultSpell(Core.Player.CurrentJob).Name, out globalCoolDown);

                if (globalCoolDown.Cooldown.TotalMilliseconds < 1000)
                    return false;
            }

            return true;
        }


        private Spell GetDefaultSpell(ClassJobType currentClass)
        {
            switch (currentClass)
            {
                case ClassJobType.Arcanist:
                    return ArcanistSpells.Ruin;
                case ClassJobType.Archer:
                    return ArcherSpells.HeavyShot;
                case ClassJobType.Bard:
                    return BardSpells.HeavyShot;
                case ClassJobType.BlackMage:
                    return BlackMageSpells.Blizzard;
                case ClassJobType.Conjurer:
                    return ConjurerSpells.Stone;
                case ClassJobType.Dragoon:
                    return DragoonSpells.TrueThrust;
                case ClassJobType.Gladiator:
                    return GladiatorSpells.FastBlade;
                case ClassJobType.Lancer:
                    return LancerSpells.TrueThrust;
                case ClassJobType.Marauder:
                    return MarauderSpells.HeavySwing;
                case ClassJobType.Monk:
                    return MonkSpells.Bootshine;
                case ClassJobType.Paladin:
                    return PaladinSpells.FastBlade;
                case ClassJobType.Pugilist:
                    return PugilistSpells.Bootshine;
                case ClassJobType.Scholar:
                    return ScholarSpells.Ruin;
                case ClassJobType.Summoner:
                    return SummonerSpells.Ruin;
                case ClassJobType.Thaumaturge:
                    return ThaumaturgeSpells.Blizzard;
                case ClassJobType.Warrior:
                    return WarriorSpells.HeavySwing;
                case ClassJobType.WhiteMage:
                    return WhiteMageSpells.Stone;
                default:
                    return null;
            }
        }

        #endregion

        #endregion
    }
}
